package com.zhangxiaofanfan.hotcode.third;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;

/**
 * TODO
 *
 * @author zhangxiaofanfan
 * @date 2024-05-24 10:33:50
 */
public class HotCode011 {
    public static void main(String[] args) {
        int[] nums = {1,3,-1,-3,5,3,6,7};
        int k = 3;
        HotCode011 hotCode = new HotCode011();
        System.out.println(Arrays.toString(hotCode.maxSlidingWindow(nums, k)));
    }

    public int[] maxSlidingWindow(int[] nums, int k) {
        if (k == 1) {
            return nums;
        }
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>((o1, o2) -> o2 - o1);
        for (int i = 0; i < k; i++) {
            priorityQueue.add(nums[i]);
        }
        int[] result = new int[nums.length - k + 1];
        for (int i = k; i < nums.length; i++) {
            result[i - k] = priorityQueue.peek();
            priorityQueue.add(nums[i]);
            priorityQueue.remove(nums[i - k]);
        }
        result[result.length - 1] = priorityQueue.peek();
        return result;
    }
}
